/**
 * Project Name:springmvc
 * File Name:TClubMemberServiceImpl.java
 * Package Name:com.yichaoart.club.biz
 * Date:2016年5月12日下午3:21:54
 * Copyright (c) 2016,  All Rights Reserved.
 *
*/

package com.yichaoart.club.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yichaoart.club.biz.TClubMemberServiceI;
import com.yichaoart.club.dal.TClubMemberMapper;
import com.yichaoart.club.model.TClubMember;

import cn.springmvc.mapper.SysFunctionMapper;
import cn.springmvc.mapper.SysRoleFunctionMapper;
import cn.springmvc.model.SysFunction;
import cn.springmvc.model.SysRoleFunction;
import cn.springmvc.model.SysRoleFunctionCriteria;
import common.CommonStant;
import utils.MD5Util;

/**
 * ClassName:TClubMemberServiceImpl <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2016年5月12日 下午3:21:54 <br/>
 * 
 * @author i1093108915@yeah.net
 * @version
 * @since JDK 1.6
 * @see
 */
@Service
public class TClubMemberServiceImpl implements TClubMemberServiceI {

	@Autowired
	TClubMemberMapper mapper;

	@Autowired
	SysRoleFunctionMapper roleFunctionMapper;

	@Autowired
	SysFunctionMapper functionMapper;

	public int insertMember(TClubMember record) {
		String recommondUser = record.getRecommendMember();
		if (org.apache.commons.lang.StringUtils.isNotEmpty(recommondUser)) {
			TClubMember member = mapper.selectByUname(recommondUser);
			if (member == null) {
				// 未找到推荐人
				record.setRecommendSid(null);
				record.setRecommendMember(null);
			} else {
				record.setRecommendSid(member.getSid());
				record.setRecommendMember(member.getUname());
				TClubMember newm = new TClubMember();
				newm.setSid(member.getSid());
				newm.setRecommondctns((member.getRecommondctns() == null ? 0 : member.getRecommondctns()) + 1);
				mapper.updateByPrimaryKeySelective(newm);
			}
		}
		String engry = MD5Util.MD5_salt(record.getPassword(), CommonStant.SALT.LOGIN_PASS_SALT);
		record.setPassword(engry);
		return mapper.insert(record);
	}

	public TClubMember selectByPrimaryKey(String sid) {
		return mapper.selectByPrimaryKey(sid);
	}

	public int updateByPrimaryKeySelective(TClubMember record) {
		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<TClubMember> queryClubMembers(int start, int limit) {
		Map<String, Object> query = new HashMap<String, Object>();
		query.put("start", start);
		query.put("limit", limit);
		mapper.queryClubMembers(query);
		return mapper.queryClubMembers(query);
	}

	@Override
	public boolean checkAuth(String email, String pass) {
		TClubMember member = mapper.selectByUname(email);
		if (null == member) {
			return false;
		}
		String encrypt = MD5Util.MD5_salt(pass, CommonStant.SALT.LOGIN_PASS_SALT);
		if (encrypt.equals(member.getPassword())) {
			return true;
		}
		return false;
	}

	@Override
	public List<SysFunction> queryNavigationByUser(String email) {
		TClubMember member = mapper.selectByUname(email);
		SysRoleFunctionCriteria rf = new SysRoleFunctionCriteria();
		SysRoleFunctionCriteria.Criteria criteria = rf.createCriteria();
		criteria.andRoleSidEqualTo(member.getCouncilMember());
		List<SysRoleFunction> rolefunctlist = roleFunctionMapper.selectByExample(rf);

		List<SysFunction> functlist = new ArrayList<SysFunction>();

		for (SysRoleFunction sysRoleFunction : rolefunctlist) {
			SysFunction function = functionMapper.selectByPrimaryKey(sysRoleFunction.getSid());
			functlist.add(function);
		}
		return functlist;
	}

	@Override
	public TClubMember selectByEmail(String email) {
		TClubMember member = mapper.selectByUname(email);
		return member;
	}

}
